<?php

namespace Biz\service\advertisement\apiModule;

use Doraemon\tools\BlockType;
use Doraemon\tools\HhzClient;
use Doraemon\tools\ImgUrl;

class HomeChannelV5 extends BaseModule
{

    const FORMAT_HIT = 'formatExtPosHitAll';

    public function format($hit, $uid, $userProfileExt, $statSignExt)
    {
        $top_list = [];
        $list = [];
        $upgradeVersion = '5.21.0';
        foreach ($hit as $ext_type => $arr) {
            foreach ($arr as $v) {
                if ($v['title'] == '装修日记' && !self::iosVersionCheck()) continue;

                if (false) {
                    $row = [
                        "title"    => $v['title'],
                        "pic"  => empty($v['pic_id']) ? '' : ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $v['pic_id'], ImgUrl::IMAGE_SIZE_OP_ORI),
                        "icon"  => empty($v['single_pic_id']) ? '' : ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $v['single_pic_id'], ImgUrl::IMAGE_SIZE_OP_ORI),
                        "link"     => $this->generateLink($v['link_type'], $v['link_params']),
                        "statSign" => $this->statSign($v, $statSignExt + [\Doraemon\service\statSign\Basic::EXTEND_KEY_KEY_WORD => $v['title']]),
                    ];
                } else {
                    $row = [
                        "title"    => $v['title'],
                        "pic_url"  => empty($v['pic_id']) ? '' : ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $v['pic_id'], ImgUrl::IMAGE_SIZE_OP_ORI),
                        "small_icon"  => empty($v['single_pic_id']) ? '' : ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $v['single_pic_id'], ImgUrl::IMAGE_SIZE_OP_ORI),
                        "link"     => $this->generateLink($v['link_type'], $v['link_params']),
                        "statSign" => $this->statSign($v, $statSignExt + [\Doraemon\service\statSign\Basic::EXTEND_KEY_KEY_WORD => $v['title']]),
                    ];
                }

                if ($ext_type == 1) {
                    $top_list[] = $row;
                } else {
                    $list[] = $row;
                }
            }
        }

        $top_list = array_slice($top_list, 0, 4);

        $top_list = \Doraemon\tools\StatSign::versionCompatible($top_list, $upgradeVersion, BlockType::TYPE_BANNER, 'banner');
        $list = \Doraemon\tools\StatSign::versionCompatible($list, $upgradeVersion, BlockType::TYPE_BANNER, 'banner');

        return compact('top_list', 'list');
    }

    //ios 小于 12 版本的用户, 使用装修日记会崩溃
    public static function iosVersionCheck()
    {
        $str = $_SERVER['HTTP_USER_AGENT'] ?? '';
        $iosVersion = "12.0.0";

        preg_match('/iOS(\s+)?(\d+)(\.\d+)?(\.\d+)?/i', $str, $matches);

        $result = true;
        $version = null;
        if (isset($matches[0])) {
            $version = strtolower($matches[0]);
            $version = str_replace("ios", "", $version);
            $version = trim($version);
            $versionArr = explode('.', $version);
            $count = count($versionArr);
            if ($count == 1) $versionArr = array_merge($versionArr, [0, 0]);
            if ($count == 2) $versionArr = array_merge($versionArr, [0]);
            $version = implode('.', $versionArr);
            if (version_compare($version, $iosVersion, "<")) $result = false;
        }

        return $result;
    }

    public function uniquePos($v): string
    {
        return $v['ext_type'] . "_" . $v['pos'];
    }

}